package com.hutianao.gqgl.file.service.impl;

import com.hutianao.gqgl.dictionary.respository.DictionaryDao;
import com.hutianao.gqgl.file.entity.Resp;
import com.hutianao.gqgl.file.repository.FileDao;
import com.hutianao.gqgl.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private FileDao fileDao;

    @Override
    public Resp<String> upload(MultipartFile file) {
        if(file.isEmpty()){
            return Resp.fail("400","文件为空!");
        }
        String OriginalFilename = file.getOriginalFilename();
        String fileName = System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
        String filePath = "D:\\Git\\";
        File dest = new File(filePath+fileName);
        if(!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
            return Resp.fail("500",OriginalFilename+"上传失败！");
        }
        return Resp.success(fileName);
    }

//    HttpHeaders headers =
//            new HttpHeaders();
// headers.setContentDispositionFormData("attachment",
//         ptFileEntity.getFileName());//告知浏览器以下载方式打开
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置 MIME
//    类型
// return new ResponseEntity<byte[]>(
//    fileByte,
//    headers,
//    HttpStatus.CREATED);
}
